package org.vineatoum.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.vineatoum.business.Exploitation;
import org.vineatoum.business.Parcelle;
import org.vineatoum.business.Plantation;
import org.vineatoum.business.action.ActionDescriptor;
import org.vineatoum.business.action.abstraction.Action;
import org.vineatoum.business.action.analyse.Prelevement;
import org.vineatoum.business.configuration.Configuration;
import org.vineatoum.business.state.Benne;
import org.vineatoum.business.state.Marc;
import org.vineatoum.business.state.Mout;
import org.vineatoum.business.state.abstraction.VinAbstract;
import org.vineatoum.business.tracking.ITrackableElement;
import org.vineatoum.model.abstraction.IActionManager;
import org.vineatoum.model.abstraction.IChaiManager;
import org.vineatoum.model.abstraction.IExploitationManager;
import org.vineatoum.model.abstraction.IVineaToumModel;
import org.vineatoum.observer.IObserverAction;
import org.vineatoum.observer.IObserverChai;
import org.vineatoum.observer.IObserverConfiguration;
import org.vineatoum.observer.IObserverExploitation;

public class VineaToumModel implements IVineaToumModel {
	IExploitationManager exploitationManager;
	IChaiManager chaiManager;
	IActionManager actionManager;
	//TODO will be auto wired
	
	List<IObserverExploitation> observersExploitation;
	List<IObserverChai> observersChai;
	List<IObserverAction> observersAction;
	List<IObserverConfiguration> observersConfiguration;
	
	public VineaToumModel() {
		try {
			JAXBContext jc = JAXBContext.newInstance(ExploitationManager.class);
			Unmarshaller unmarshaller = jc.createUnmarshaller();
			exploitationManager = (ExploitationManager) unmarshaller.unmarshal(getClass().getResource("/exploitation.xml"));
			
		} catch (JAXBException e) {
			exploitationManager = new ExploitationManager();
			e.printStackTrace();
		}

		chaiManager = new ChaiManager();
		actionManager = new ActionManager();
		
		observersExploitation = new ArrayList<IObserverExploitation>();
		observersChai = new ArrayList<IObserverChai>();
		observersAction = new ArrayList<IObserverAction>();
		observersConfiguration = new ArrayList<IObserverConfiguration>();
		
		
		/*
		 * DATA FOR TEST. TO REMOVE WHEN PERSITENCE IS IMPLEMENTED 
		 */
		Plantation plantation = Plantation.plantations.get(0);
		
//TODO		Raisin raisin = plantation.getVintage(2011);
		
		Prelevement prelevement = new Prelevement();
		prelevement.setAcidity((float) 4.2);
		prelevement.setComment("No comment");
		prelevement.setDate(new Date());
		prelevement.setMaturity(11);
		prelevement.setEstimatedYield(55);
		
		plantation.addAnalyse(prelevement);
		
		Benne sala = new Benne();
		sala.setName("Benne 1");
		sala.setAcidity(5);
		sala.setDensity(12);
		sala.setQuantity(60);
		sala.addNewOrigines(plantation);
		plantation.addNewFils(sala);
		
		Benne guigui = new Benne();
		guigui.setName("Benne 2");
		guigui.setAcidity(4);
		guigui.setDensity(13);
		guigui.setQuantity(55);
		guigui.addNewOrigines(plantation);
		plantation.addNewFils(guigui);
		
		Marc lavoine = new Marc();
		lavoine.setName("GAMAY 1");
		sala.addNewFils(lavoine);
		lavoine.addNewOrigines(sala);
		
		Mout mout = new Mout();
		mout.setName("GAMAY MOUT 1");
		lavoine.addNewFils(mout);
		mout.addNewOrigines(lavoine);
	}

	@Override
	public void addAction(ActionDescriptor action) {
		// TODO Auto-generated method stub
	}

	@Override
	public void addCuve() {
		// TODO Auto-generated method stub
	}

	@Override
	public void addParcelle(Parcelle parcelle) {
		// TODO Auto-generated method stub
	}

	@Override
	public void addPlantation(Plantation plantation) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<Action> getActions(VinAbstract vin) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<ITrackableElement> getTracking(VinAbstract vin) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void updateExploitation(Exploitation exploitation) {
		exploitationManager.updateExploitation(exploitation);
	}
	
	@Override
	public Exploitation getExploitation() {
		return exploitationManager.getExploitation();
	}

	@Override
	public Configuration getConfiguration() {
		return exploitationManager.getConfiguration();
	}

	@Override
	public void updateConfiguration(Configuration config) {
		exploitationManager.updateConfiguration(config);
	}

	/* 
	 * OBSERVER - OBSERVABLE
	 */

	@Override
	public void addObserver(IObserverAction o) {
		observersAction.add(o);
	}

	@Override
	public void deleteObserver(IObserverAction o) {
		observersAction.remove(o);
	}

	@Override
	public void addObserver(IObserverChai o) {
		observersChai.add(o);
	}

	@Override
	public void deleteObserver(IObserverChai o) {
		observersChai.remove(o);
	}

	@Override
	public void addObserver(IObserverExploitation o) {
		observersExploitation.add(o);
	}

	@Override
	public void deleteObserver(IObserverExploitation o) {
		observersExploitation.remove(o);
	}
	
	@Override
	public void addObserver(IObserverConfiguration o) {
		observersConfiguration.add(o);
	}

	@Override
	public void deleteObserver(IObserverConfiguration o) {
		observersConfiguration.remove(o);
		
	}
}
